package com.global.college.utils;


import androidx.fragment.app.FragmentActivity;

import com.global.college.ui.activity.SplashActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * Created by fu on 2020/2/20.
 * Describe: 应用程序Activity管理类：用于Activity管理和应用程序退出
 */
public class AppManagerUtils {

    private static List<FragmentActivity> activitys = new ArrayList<FragmentActivity>();
    private static AppManagerUtils instance;

    private static SplashActivity splashActivity;


    /**
     * 获取单一实例
     */
    public static AppManagerUtils getInstance() {
        if (instance == null) {
            instance = new AppManagerUtils();
        }
        return instance;
    }

    /**
     * 添加Activity到堆栈
     */
    public void addActivity(FragmentActivity activity) {
        if (activitys == null) {
            activitys = new Stack<FragmentActivity>();
        }
        activitys.add(activity);
    }

    /**
     * 获取当前Activity（堆栈中最后一个压入的）
     */
    public FragmentActivity currentActivity() {
        return activitys == null || activitys.size() == 0 ? null : activitys.get(activitys.size() - 1);
    }

    /**
     * 结束当前Activity（堆栈中最后一个压入的）
     */
    public void finishActivity() {
        finishActivity(currentActivity());
    }

    /**
     * 结束指定的Activity
     */
    public void finishActivity(FragmentActivity activity) {
        if (activity != null) {
            activity.finish();
            activitys.remove(activity);
            activity = null;
        }
    }

    public void setSplashActivity(SplashActivity activity) {
            splashActivity = activity;
    }

    public void finishSpalshActivity() {
        if (splashActivity != null) {
            splashActivity.finish();
            splashActivity = null;
        }
    }

    /**
     * 结束指定类名的Activity
     */
    public void finishActivity(Class<?> cls) {
        for (FragmentActivity activity : activitys) {
            if (activity.getClass().equals(cls)) {
                finishActivity(activity);
                break;
            }
        }
    }

    /**
     * 结束所有Activity
     */
    public void finishAllActivity() {
        for (int i = activitys.size() - 1; i >= 0; i--) {
            if (null != activitys.get(i)) {
                FragmentActivity activity = activitys.get(i);
                activity.finish();
                activity = null;
            }
        }
        activitys.clear();
    }

    /**
     * 关闭除了指定activity以外的全部activity 如果cls不存在于栈中，则栈全部清空
     */
    public void finishOthersActivity(Class<?> cls) {
        for (FragmentActivity activity : activitys) {
            if (!(activity.getClass().equals(cls))) {
                finishActivity(activity);
            }
        }
    }

    /**
     * 获取指定的Activity
     */
    @SuppressWarnings("unchecked")
    public <T extends FragmentActivity> T getActivity(Class<? extends FragmentActivity> cls) {
        for (FragmentActivity activity : activitys) {
            if (activity.getClass().equals(cls)) {
                return (T) activity;
            }
        }
        return null;
    }

    /**
     * 退出应用程序
     */
    public void exit() {
        try {
            finishAllActivity();
            System.exit(0);
            android.os.Process.killProcess(android.os.Process.myPid());
        } catch (Exception e) {
            System.exit(-1);
        }
    }

    public int getActivitySize() {
        return activitys.size();
    }

}
